package com.wzy.service.user.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wzy.common.base.jwtToken.JwtInfo;
import com.wzy.common.base.jwtToken.JwtUtils;
import com.wzy.common.base.result.R;
import com.wzy.common.base.result.ResultCodeEnum;
import com.wzy.service.user.dto.LoginParam;
import com.wzy.service.user.dto.PerfectInformationParam;
import com.wzy.service.user.dto.RegisterParam;
import com.wzy.service.user.dto.vo.UserInformationQueryVo;
import com.wzy.service.user.entity.UserInformation;
import com.wzy.service.user.service.UserInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuzhengyu
 * @since 2021-03-13
 */
@RestController
@RequestMapping("/user")
public class UserInformationController {

    @Autowired
    private UserInformationService userInformationService;


    @GetMapping("/test/information")
    public R getInformationByUId(@RequestParam String uid) {
        UserInformation byId = userInformationService.getById(uid);
        return R.ok().data("user", byId);
    }

    @RequestMapping(path= "/test/login", method = RequestMethod.POST)
    public R testLogin() {
        return R.ok().data("token", "admin");
    }

    @GetMapping("testuser")
    public R testUser() {
        return R.ok();
    }

    @RequestMapping(path = "/user-information/{uid}", method = RequestMethod.GET)
        public R getInformationById(@PathVariable String uid) {
        UserInformation userInformation = userInformationService.getById(uid);
        return R.ok().data("information", userInformation);
    }


    @GetMapping("/list")
    public R list() {
        List<UserInformation> list = userInformationService.exceptAdminList();
        return R.ok().data("list", list);
    }

    @GetMapping("list/{page}/{limit}")
    public R listPage(@PathVariable Long page, @PathVariable Long limit, UserInformationQueryVo userInformationQueryVo) {
        System.out.println(userInformationQueryVo);
        IPage<UserInformation> pageModel = userInformationService.selectPage(page,limit,userInformationQueryVo);
        List<UserInformation> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    @DeleteMapping("/information/{id}")
    public R removeById(@PathVariable String id) {
        boolean isSuccess = userInformationService.removeById(id);
        if(isSuccess) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping(path= "/test/info", method = RequestMethod.GET)
    public R info(String token) {
        //JwtUtils.getMemberIdByJwtToken()
        //UserInformation user = userInformationService.getById(id);
        // 解析token,获取到tokenInfo,将其返回给前端
        JwtInfo info = JwtUtils.getMemberIdByJwtToken(token);
        return R.ok().data("name", info.getNickname()).data("avatar", info.getAvatar()).data("id", info.getId());
    }

    @RequestMapping(path= "/test/logout", method = RequestMethod.POST)
    public R logout() {
        return R.ok();
    }


    // 获取验证码
    @RequestMapping(path = {"/verification"}, method = RequestMethod.GET)
    public R getVerificationCode(@RequestParam("") String phone) {
        if(phone == null) {
            return R.error(ResultCodeEnum.PARAM_ERROR);
        }
        return userInformationService.getVerificationCode(phone);
    }

    /**
     * 注册
     * @return
     */
    @RequestMapping(path = "information", method = RequestMethod.POST)
    public R register(@RequestBody RegisterParam userParam) {
        return userInformationService.register(userParam);
    }

    @RequestMapping(path = "login", method = RequestMethod.POST)
    public R loginPassword(@RequestBody Map<String, String> map){

        // 判断登录, 生成token
        return userInformationService.loginPassword(map.get("username"), map.get("password"));
    }




    /**
     * 验证码登录
     * @param loginParam
     * @return
     */
    @RequestMapping(path = "login-verification", method = RequestMethod.POST)
    public R loginVerification(@RequestBody LoginParam loginParam){
        return userInformationService.loginVerification(loginParam.getPhone(),loginParam.getVerificationCode());
    }

    /**
     * 用户信息完善
     * @param param
     * @return
     */
    @RequestMapping(path = "perfect-information" ,method = RequestMethod.PUT)
    public R perfectInformation(@RequestBody PerfectInformationParam param) {
        return userInformationService.perfectInformation(param);
    }

    @RequestMapping(path = "information", method = RequestMethod.PUT)
    public R information(@RequestBody UserInformation userInformation) {
        userInformation.setGmtModified(new Date());
        boolean isSuccess = userInformationService.updateById(userInformation);
        if (isSuccess) {
            return R.ok();
        } else {
            return R.error();
        }
    }



}

